{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0]\n"
     ]
    }
   ],
   "source": [
    "knn = KNeighborsClassifier(n_neighbors=2)# KNN分类模型，K值为2\n",
    "X = [[2, 1], [3, 1], [1, 4], [2, 6]]# 特征\n",
    "y = [0, 0, 1, 1]# 标签\n",
    "knn.fit(X, y)# 模型训练\n",
    "arr=knn.predict([[2, 2]])# 预测\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsRegressor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.5])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn = KNeighborsRegressor(n_neighbors=2)# KNN回归模型，K值为2\n",
    "X = [[2, 1], [3, 1], [1, 4], [2, 6]]# 特征\n",
    "y = [0.5, 0.33, 4, 3]# 标签\n",
    "knn.fit(X, y)# 模型训练\n",
    "knn.predict([[4, 9]])# 预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [2]\n",
      " [3]\n",
      " [4]\n",
      " [5]]\n",
      "斜率 w = 8.500000000000002\n",
      "截距 b = 55.49999999999999\n",
      "线性方程：y = 8.500000000000002x + 55.49999999999999\n",
      "残差平方和 = 47.49999999999994\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from sklearn.linear_model import LinearRegression\n",
    "\n",
    "# 1. 准备数据\n",
    "x = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)  # 特征（需为二维数组）\n",
    "print(x)\n",
    "y = np.array([60, 75, 85, 90, 95])            # 标签\n",
    "\n",
    "# 2. 用sklearn的线性回归模型（底层用最小二乘法）\n",
    "model = LinearRegression()\n",
    "model.fit(x, y)\n",
    "\n",
    "# 3. 输出参数\n",
    "w = model.coef_[0]  # 斜率\n",
    "b = model.intercept_  # 截距\n",
    "print(f\"斜率 w = {w}\")\n",
    "print(f\"截距 b = {b}\")\n",
    "print(f\"线性方程：y = {w}x + {b}\")\n",
    "\n",
    "# 4. 验证残差平方和\n",
    "y_pred = model.predict(x)\n",
    "residual_sum = np.sum((y - y_pred)**2)\n",
    "print(f\"残差平方和 = {residual_sum}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.         -0.94280904]\n",
      " [ 1.41421356 -0.94280904]\n",
      " [-1.41421356  0.47140452]\n",
      " [ 0.          1.41421356]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "X = [[2, 1], [3, 1], [1, 4], [2, 6]]\n",
    "# 标准化\n",
    "X = StandardScaler().fit_transform(X)\n",
    "print(X)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
